package com.rois.librois.libRoISMessageStorage;

import java.util.Vector;

import com.rois.librois.libRoISComponentClient.ClientOperationImpl;

/**
 * Data holder class for RoIS Component.
 * This object holds client object and a list of:
 * - connection status
 * - connecting CORBA server name
 * - CORBA naming service
 * - Component name (Component type)
 * - Component ID
 * - exchanged command messages
 * - exchanged query messages
 * - exchanged event messages
 * - exchanged error message
 * - Component parameters
 *
 * @author Miki Sato <miki.sato.1225@gmail.com>
 *
 *    The MIT licence
 *
 *    Copyright (C) 2011
 *      ATR Intelligent Robotics and Communication Laboratories, Japan
 *
 *    Permission is hereby granted, free of charge, to any person obtaining a copy
 *    of this software and associated documentation files (the "Software"), to deal
 *    in the Software without restriction, including without limitation the rights
 *    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 *    of the Software, and to permit persons to whom the Software is furnished to do so,
 *    subject to the following conditions:
 *
 *    The above copyright notice and this permission notice shall be included in all
 *    copies or substantial portions of the Software.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 *    INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *    PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 *    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

public class RoIS_Components {
	private static final int DEFAULT_COMP_MAXSIZE = 100;
	private static final int DEFAULT_MSG_MAXSIZE = 100;
	private static final int DEFAULT_TYPE_MAXSIZE = 100;
	public ClientOperationImpl client_p;
	public int size;
	public int max_size;
	public int max_cmd_size;
	public int max_qry_size;
	public int max_evt_size;
	public int max_err_size;
	public int max_err_message_size;

	public Vector<Boolean> connection = new Vector<Boolean>();
	public Vector<String> nameservice = new Vector<String>();
	public Vector<String> servername = new Vector<String>();
	public Vector<String> name = new Vector<String>();
	public Vector<String> referenceid = new Vector<String>();

	public Vector<RoIS_ExchangedMessages> command_message = new Vector<RoIS_ExchangedMessages>();
	public Vector<RoIS_ExchangedMessages> query_message = new Vector<RoIS_ExchangedMessages>();
	public Vector<RoIS_ExchangedMessages> event_message = new Vector<RoIS_ExchangedMessages>();
	public Vector<RoIS_RegisteredTypes> error_message = new Vector<RoIS_RegisteredTypes>();
	public Vector<RoIS_Parameters> parameter = new Vector<RoIS_Parameters>();

	/**
	 * create RoIS Component data holder.
	 */
	public RoIS_Components()
	{
		client_p = null;
		max_size = DEFAULT_COMP_MAXSIZE;
		max_cmd_size = DEFAULT_MSG_MAXSIZE;
		max_qry_size = DEFAULT_MSG_MAXSIZE;
		max_evt_size = DEFAULT_MSG_MAXSIZE;
		max_err_size = DEFAULT_TYPE_MAXSIZE;
		max_err_message_size = DEFAULT_MSG_MAXSIZE;
		clear();
	}

	/**
	 * clear all data.
	 */
	public void clear()
	{
		if(client_p!=null)
		{
			client_p = null;
		}
		client_p = new ClientOperationImpl();

		size = 0;
		connection.clear();
		nameservice.clear();
		servername.clear();
		name.clear();
		referenceid.clear();

		command_message.clear();
		query_message.clear();
		event_message.clear();
		error_message.clear();
		parameter.clear();
	}

	/**
	 * add Component at the end of component list.
	 * @param _connection connection status
	 * @param _nameservice CORBA naming service
	 * @param _servername CORBA server name of the component
	 * @param _name component name of the component
	 * @param _referenceid reference ID of the component
	 */
	public void push_back(
		boolean _connection,
		String _nameservice,
		String _servername,
		String _name,
		String _referenceid
	)
	{
		connection.add(_connection);
		nameservice.add(_nameservice);
		servername.add(_servername);
		name.add(_name);
		referenceid.add(_referenceid);

		RoIS_ExchangedMessages _command_message = new RoIS_ExchangedMessages();
		_command_message.clear();
		_command_message.max_size = max_cmd_size;
		command_message.add(_command_message);

		RoIS_ExchangedMessages _query_message = new RoIS_ExchangedMessages();
		_query_message.clear();
		_query_message.max_size = max_qry_size;
		query_message.add(_query_message);

		RoIS_ExchangedMessages _event_message = new RoIS_ExchangedMessages();
		_event_message.clear();
		_event_message.max_size = max_evt_size;
		event_message.add(_event_message);

		RoIS_RegisteredTypes _error_message = new RoIS_RegisteredTypes();
		_error_message.clear();
		_error_message.max_size = max_err_size;
		_error_message.max_message_size = max_err_message_size;
		error_message.add(_error_message);

		RoIS_Parameters _parameter = new RoIS_Parameters();
		parameter.add(_parameter);

		size++;
		if(size>max_size)
		{
			erase(0);
		}
	}

	/**
	 * remove Component at the end of component list.
	 */
	public void pop_back()
	{
		if(size>0)
		{
			connection.remove(connection.size()-1);
			nameservice.remove(nameservice.size()-1);
			servername.remove(servername.size()-1);
			name.remove(name.size()-1);
			referenceid.remove(referenceid.size()-1);

			command_message.remove(command_message.size()-1);
			query_message.remove(query_message.size()-1);
			event_message.remove(event_message.size()-1);
			error_message.remove(error_message.size()-1);
			parameter.remove(parameter.size()-1);

			size--;
		}
	}

	/**
	 * remove specified component from component list.
	 * @param index index of the component
	 */
	public void erase(
		int index
	)
	{
		if(size>0)
		{
			connection.remove(index);
			nameservice.remove(index);
			servername.remove(index);
			name.remove(index);
			referenceid.remove(index);

			command_message.remove(index);
			query_message.remove(index);
			event_message.remove(index);
			error_message.remove(index);
			parameter.remove(index);

			size--;
		}
	}

	/**
	 * get index of a component related to server name.
	 * @param _servername CORBA server name of the component
	 * @return index of the component
	 */
	public int getIndexByServername(
		String _servername
	)
	{
		int iRes = -1;
		for(int i=0; i<size; i++)
		{
			if(servername.get(i).matches(_servername))
			{
				iRes = i;
				break;
			}
		}
		return iRes;
	}

	/**
	 * get index of a component related to component ID.
	 * @param _referenceid reference ID of the component
	 * @return index of the component
	 */
	public int getIndexByReferenceid(
		String _referenceid
	)
	{
		int iRes = -1;
		for(int i=0; i<size; i++)
		{
			if(referenceid.get(i).matches(_referenceid))
			{
				iRes = i;
				break;
			}
		}
		return iRes;
	}

	/**
	 * get server name related to index.
	 * @param index index of the component
	 * @return server name of the component
	 */
	public String getServernameByIndex(
		int index
	)
	{
		String strRes = "";
		if(0<=index && index<size)
		{
			strRes = servername.get(index);
		}
		return strRes;
	}

	/**
	 * get server name related to reference ID
	 * @param _referenceid reference ID of the component
	 * @return server name of the component
	 */
	public String getServernameByReferenceid(
		String _referenceid
	)
	{
		String strRes = "";
		for(int i=0; i<size; i++)
		{
			if(referenceid.get(i).matches(_referenceid))
			{
				strRes = servername.get(i);
				break;
			}
		}
		return strRes;
	}
}
